<?php
function GetStrEinheit($einheit, $short=true) {
	switch ($einheit) {
	   case 1:
	      if ($short)
	         $sein = "S";
	      else
	         $sein = "Stunde";
	   break;

	   case 2:
	      if ($short)
	         $sein = "T";
	      else
	         $sein = "Tage";
	   break;

	   case 3:
	      if ($short)
	         $sein = "W";
	      else
	         $sein = "Wochen";
	   break;

	   case 4:
	      if ($short)
	         $sein = "M";
	      else
	         $sein = "Monate";
	   break;

	   case 5:
	      if ($short)
	         $sein = "Q";
	      else
	         $sein = "Quartale";
	   break;

	   case 6:
	      if ($short)
	         $sein = "J";
	      else
	         $sein = "Jahre";
	   break;

	   default: $sein = "NaN";
	}

	return $sein;
}

function GetStrMonth($mon, $short=true) {
	switch ($mon) {
	   case 1:
	      if ($short)
	         $sm = "Jan";
	      else
	         $sm = "J%auml;nner";
	   break;

	   case 2:
	      if ($short)
	         $sm = "Feb";
	      else
	         $sm = "Februar";
	   break;

	   case 3:
	      if ($short)
	         $sm = "M&auml;r";
	      else
	         $sm = "M&auml;rz";
	   break;

	   case 4:
	      if ($short)
	         $sm = "Apr";
	      else
	         $sm = "April";
	   break;

	   case 5:
	         $sm = "Mai";
	   break;

	   case 6:
	      if ($short)
	         $sm = "Jun";
	      else
	         $sm = "Juni";
	   break;

	   case 7:
	      if ($short)
	         $sm = "Jul";
	      else
	         $sm = "Juli";
	   break;

	   case 8:
	      if ($short)
	         $sm = "Aug";
	      else
	         $sm = "August";
	   break;

	   case 9:
	      if ($short)
	         $sm = "Sep";
	      else
	         $sm = "September";
	   break;

	   case 10:
	      if ($short)
	         $sm = "Okt";
	      else
	         $sm = "Oktober";
	   break;

	   case 11:
	      if ($short)
	         $sm = "Nov";
	      else
	         $sm = "November";
	   break;

	   case 12:
	      if ($short)
	         $sm = "Dez";
	      else
	         $sm = "Dezember";
	   break;

	   default: $sm = "NaN";
	}

	return $sm;
}

# Errechne das Endedatum ausgehend von einem gegebenen Datum, abhaengig
# von der eingestellten Einheit.
#
function GetEDate($einheit, $dt, $len) {
	switch ($einheit) {
	   case 1: $edate = $dt + (3600 * $len); break;
	   case 2: $edate = $dt + (86400 * $len); break;
	   case 3: $edate = $dt + ((86400 * 7) * $len); break;
	   case 4: $edate = $dt + ((86400 * 30) * $len); break;
	   case 5: $edate = $dt + ((86400 * 91) * $len); break;
	   case 6: $edate = $dt + ((86400 * 364) * $len); break;
	}

	return $edate;
}

# Setze den Tag immer auf den Letzten der eingestellten Periode.
#
function GetUltimo($dt) {
	global $periode;

	$day = gmdate("j", $dt);
	$mon = gmdate("n", $dt);
	$year = gmdate("Y", $dt);

	switch($periode) {
	   case 1:			// Jahr
	      $dat = gmmktime(0, 0, 0, 12, 31, $year);
	   break;

	   case 2:			// Quartal
	      if ($mon <= 3)
	         $dat = gmmktime(0, 0, 0, 3, daysinmonth(3, $year), $year);
	      else if ($mon <= 6)
	         $dat = gmmktime(0, 0, 0, 6, daysinmonth(6, $year), $year);
	      else if ($mon <= 9)
	         $dat = gmmktime(0, 0, 0, 9, daysinmonth(9, $year), $year);
	      else
	         $dat = gmmktime(0, 0, 0, 12, 31, $year);
	   break;

	   case 3:			// Monat
	      $dat = gmmktime(0, 0, 0, $mon, daysinmonth($mon, $year), $year);
	   break;

	   case 4:			// Woche
	      $darr = getdate($dt);
	      $wday = $darr["wday"];

	      if ($wday == 0)
	         $wday = 7;

	      $miss = 7 - $wday;
	      $j = 0;

	      while ($j < $miss) {
	         $day++;

		 if (!checkdate($mon, $day, $year)) {
		    $day = 1;
		    $mon++;

		    if ($mon > 12) {
		       $mon = 1;
		       $year++;
		    }
		 }

		 $j++;
	      }

	      $dat = gmmktime(0, 0, 0, $mon, $day, $year);
	   break;

	   case 5:			// Tag
	      $dat = $dt;
	   break;
	}

	return $dat;
}

# Erhoehe das Datum um die laenge der angegebenen Periode.
#
function IncDate($periode, $dt) {
	$amon = array(31,28,31,30,31,30,31,31,30,31,30,31);

	$tm = gmdate("j.n.Y", $dt);
	$ad = explode(".", $tm);

	switch ($periode) {
	   case 1:			// Jahr
	      $ad[2]++;
	   break;

	   case 2:			// Quartal
	      $ad[1] += 3;

	      if ($ad[1] > 12) {
	         $ad[1] = 12 - $ad[1];
		 $ad[2]++;
	      }
	   break;

	   case 3:			// Monat
	      $ad[1]++;

	      if ($ad[1] > 12) {
		 $ad[1] = 1;
		 $ad[2]++;
	      }

	      if (!checkdate($ad[1], $ad[0], $ad[2]) && $ad[1] > 28)
	         $ad[0] = daysinmonth($ad[1], $ad[2]);
	      else
	         $ad[0] = 1;
	   break;

	   case 4:			// Woche
	      $ad[0] += 7;

	      if ($ad[0] > daysinmonth($ad[1], $ad[2])) {
		 $ad[0] = 7 - $ad[0];
		 $ad[1]++;

	         if ($ad[1] > 12) {
		    $ad[1] = 1;
		    $ad[2]++;
		 }
	      }
	   break;

	   case 5:			// Tag
	      $ad[0]++;

	      if ($ad[0] > daysinmonth($ad[1], $ad[2])) {
	         $ad[0] = 1;
		 $ad[1]++;

		 if ($ad[1] > 12) {
	            $ad[1] = 1;
		    $ad[2]++;
		 }
	      }
	   break;
	}

	$dt = gmmktime(0, 0, 0, $ad[1], $ad[0], $ad[2]);
	return $dt;
}

# Vergleiche zwei Datum abhaengig von der eingestellten Periode. Aehnlich
# wie die Funktion strcmp() wird das Datum1 vom Datum2 abgezogen und das
# Ergebnis an die aufrufende Funktion zurueckgegeben.
#
function PerDateComp($dt1, $dt2) {
	global $periode;

	$day = gmdate("j", $dt1);
	$mon = gmdate("n", $dt1);
	$year = gmdate("Y", $dt1);
	$ndt1 = gmmktime(0, 0, 0, $mon, $day, $year);
	$day = gmdate("j", $dt2);
	$mon = gmdate("n", $dt2);
	$year = gmdate("Y", $dt2);
	$ndt2 = gmmktime(0, 0, 0, $mon, $day, $year);

	if ($periode == 5 || $periode == 4) {	// Tag, Woche
	   return $ndt2 - $ndt1;
	}

	if ($periode == 3 || $periode == 2) {	// Monat, Quartal
	   $dst1 = gmdate("j.n.Y", $ndt1);
	   $dst2 = gmdate("j.n.Y", $ndt2);
	   $da1 = explode(".", $dst1);
	   $da2 = explode(".", $dst2);
	   $ndt1 = gmmktime(0, 0, 0, $da1[1], 1, $da1[2]);
	   $ndt2 = gmmktime(0, 0, 0, $da2[1], 1, $da2[2]);
	   return $ndt2 - $ndt1;
	}

	$dst1 = gmdate("Y", $dt1);
	$dst2 = gmdate("Y", $dt2);
	return $dst2 - $dst1;
}

# folgende Funktion gibt die Periode zurueck, in die das uebergebene Datum
# faellt.
#
function GetPeriode($datum) {
	global $periode;

	$tm = gmdate("j.n.Y", $datum);
	$dt = explode(".", $tm);

	switch ($periode) {
	   case 1:		// Jahr
	      return $dt[2];
	   break;

	   case 2:		// Quartal
	      $xx = $dt[1] / 3;

	      if (($dt[1] % 3) != 0)
	         $xx += 1;

	      return (integer)$xx;
	   break;

	   case 3:		// Monat
	      return $dt[1];
	   break;

	   case 4:		// Woche
	      $xa = getdate($datum);
	      $xx = $xa["yday"] / 7;

	      if (($xa["yday"] % 7) != 0)
	         $xx += 1;

	      return (integer)$xx;
	   break;

	   case 5:		// Tag
	      $xa = getdate($datum);
	      return $xa["yday"];
	   break;
	}

	return 0;
}

function daysinmonth($month, $year)
{
   $days = 31;
   while($days > 0 && !checkdate($month, $days, $year)) $days--;
   return $days;
}

function GetDateRange($per, $fdate, $ldate) {
	 global $periode;

	 $mon = gmdate("n", $per);
	 $year = gmdate("Y", $per);
	 $days = 0;

	 switch ($periode) {
	    case 1:	    // Jahr
		  $fdate = gmmktime(0, 0, 0, 1, 1, $year);
		  $ldate = gmmktime(0, 0, 0, 12, 31, $year);
            break;

            case 2:	   // Quartal
               switch ($mon) {
                  case 1:
                  case 2:
                  case 3:
                     $days = daysinmonth(3, $year);
		     $fdate = gmmktime(0, 0, 0, 1, 1, $year);
		     $ldate = gmmktime(0, 0, 0, 3, $days, $year);
                  break;

                  case 4:
                  case 5:
                  case 6:
                     $days = daysinmonth(6, $year);
		     $fdate = gmmktime(0, 0, 0, 4, 1, $year);
		     $ldate = gmmktime(0, 0, 0, 6, $days, $year);
                  break;

                  case 7:
                  case 8:
                  case 9:
                     $days = daysinmonth(9, $year);
		     $fdate = gmmktime(0, 0, 0, 7, 1, $year);
		     $ldate = gmmktime(0, 0, 0, 9, $days, $year);
                  break;

                  case 10:
                  case 11:
                  case 12:
		     $fdate = gmmktime(0, 0, 0, 10, 1, $year);
		     $ldate = gmmktime(0, 0, 0, 12, 31, $year);
                  break;
              }
           break;

           case 3:	      // Monat
              $days = daysinmonth($mon, $year);
	      $fdate = gmmktime(0, 0, 0, $mon, 1, $year);
	      $ldate = gmmktime(0, 0, 0, $mon, $days, $year);
           break;

           case 4:    		// Woche
              $dt = getdate($per);
              $tag = gmdate("j", $per);
              $fdate = gmmktime(0, 0, 0, $mon, $tag - $dt['wday'], $year);
              $ldate = gmmktime(0, 0, 0, $mon, ($tag - $dt['wday']) + 7, $year);
           break;

           case 5:    	 	// Tag
              $fdate = $per;
              $ldate = $per;
           break;
	}

	$days = ($ldate - $fdate) / 86400;
	return $days;
}

# Folgende Funktion liefert den Abstand zwischen zwei Datum, abhaengig von
# der Periode und dem Kennzeichen $pmlight. Ist Letzteres gesetzt, wird
# nur eine Periodengenaue Eingabe ermoeglicht, andernfalls eine Tagesgenaue.
#
function GetAnzEntries($von, $bis) {
	global $periode;
	global $pmlight;

	$day = gmdate("j", $von);
	$mon = gmdate("n", $von);
	$year = gmdate("Y", $von);
	$nvon = gmmktime(0, 0, 0, $mon, $day, $year);
	$day = gmdate("j", $bis);
	$mon = gmdate("n", $bis);
	$year = gmdate("Y", $bis);
	$nbis = gmmktime(0, 0, 0, $mon, $day, $year);

	if ($nvon > $nbis)
	   return 0;

	if ($nvon == $nbis)
	   return 1;

	if ($pmlight) {
	   $vmon = gmdate("n", $nvon);
	   $bmon = gmdate("n", $nbis);

	   if ($periode == 2) {
	      if ($vmon >= 1 && $vmon <= 3)
	         $vmon = 1;
	      else if ($vmon >= 4 && $vmon <= 6)
	         $vmon = 4;
	      else if ($vmon >= 7 && $vmon <= 9)
	         $vmon = 7;
	      else
	         $vmon = 10;

	      if ($bmon >= 1 && $bmon <= 3)
	         $bmon = 3;
	      else if ($vmon >= 4 && $vmon <= 6)
	         $vmon = 6;
	      else if ($vmon >= 7 && $vmon <= 9)
	         $vmon = 9;
	      else
	         $vmon = 12;
	   }

	   $vyear = gmdate("Y", $nvon);
	   $byear = gmdate("Y", $nbis);
	   $r = 1;

	   while ($vmon != $bmon || $vyear != $byear) {
	      if ($periode == 2)
	         $vmon += 3;
	      else if ($periode == 1)
	         $year++;
	      else
	         $vmon++;

	      if ($vmon > 12) {
		 $vyear++;
		 $vmon = 1;
	      }

	      $r++;
	   }

	   return $r;
	}

	# Vollversion ist immer Taggenau!
	# Aufgrund der Sommerzeit wird hier scheinbar kryptisch verfahren.
	# FIXME! Laufzeiten ueber mehrere Jahre funtionieren nicht!!!
	#
	$yday_von = gmstrftime("%j", $nvon);
	$yday_bis = gmstrftime("%j", $nbis);
	$year_von = gmdate("Y", $nvon);
	$year_bis = gmdate("Y", $nbis);

	if ($year_von == $year_bis)
	   return $yday_bis - $yday_von + 1;
	else {
	   $mon_von = gmdate("n", $nvon);
	   $mon_bis = gmdate("n", $nbis);
	   $yend = gmmktime(0, 0, 0, 12, 31, $year_von);
	   $yday_end = gmstrftime("%j", $yend);
	   $diff = ($yday_end - $yday_von) + $yday_bis + 1;
	}

	return $diff;
}

# Folgende Funktion gibt einen String zurueck, welcher in Bildschirmmasken
# dazu verwendet wird um dem Anwender zu zeigen wie er das Datum
# einzugeben hat.
#
function GetVisualDate ($dt, $pl = false) {
	if (!$pl) {
	   switch ($dt) {
	      case "j.n.Y":
	      case "d.m.Y":
	         return "TT.MM.JJJJ";
	      break;

	      case "j.n.y":
	      case "d.m.y":
	         return "TT.MM.JJ";
	      break;

	      case "Y-m-d":
	         return "YYYY-MM-DD";
	      break;

	      case "m/d/Y":
	         return "MM/DD/YYYY";
	      break;

	      case "m/d/y":
	         return "MM/DD/YY";
	      break;
	   }
	} else {
	   switch ($dt) {
	      case "j.n.Y":
	      case "d.m.Y":
	         return "MM.JJJJ";
	      break;

	      case "j.n.y":
	      case "d.m.y":
	         return "MM.JJ";
	      break;

	      case "Y-m-d":
	         return "YYYY-MM";
	      break;

	      case "m/d/Y":
	         return "MM/YYYY";
	      break;

	      case "m/d/y":
	         return "MM/YY";
	      break;
	   }
	}

	return "???";
}

function GetDBDateFormat ($time=false, $pl=false) {
	global $dtformatshort;

	$str = "";

	if (!$pl) {
	   switch ($dtformatshort) {
	      case "j.n.Y":
	      case "d.m.Y":
		 $str = "DD.MM.YYYY";
		 if ($time) $str .= " HH24:MI";
	      break;

	      case "j.n.y":
	      case "d.m.y":
		 $str = "DD.MM.YY";
		 if ($time) $str .= " HH24:MI";
	      break;

	      case "Y-m-d":
		 $str = "YYYY-MM-DD";
		 if ($time) $str .= " HH24:MI";
	      break;

	      case "m/d/Y":
		 $str = "MM/DD/YYYY";
		 if ($time) $str .= " HH24:MI";
	      break;

	      case "m/d/y":
		 $str = "MM/DD/YYYY";
		 if ($time) $str .= " HH24:MI";
	      break;

	      default:
		 $str = "YYYY-MM-DD";
		 if ($time) $str .= " HH24:MI";
	   }
	} else {
	   switch ($dtformatshort) {
	      case "j.n.Y":
	      case "d.m.Y":
	         return "MM.YYYY";
	      break;

	      case "j.n.y":
	      case "d.m.y":
	         return "MM.YY";
	      break;

	      case "Y-m-d":
	         return "YYYY-MM";
	      break;

	      case "m/d/Y":
	         return "MM/YYYY";
	      break;

	      case "m/d/y":
	         return "MM/YY";
	      break;

	      default:
		 $str = "YYYY-MM-DD";
		 if ($time) $str .= " HH24:MI";
	   }
	}

	return $str;
}

function DateToDBDate($dt) {
	global $dtformatshort;

	switch ($dtformatshort) {
	   case "j.n.Y":
	   case "d.m.Y":
	      $darr = explode(".", $dt);
	      $str = sprintf("%04d-%02d-%02d", $darr[2], $darr[1], $darr[0]);
	   break;

	   case "j.n.y":
	   case "d.m.y":
	      $darr = explode(".", $dt);

	      if ($darr[2] < 50)
		 $year = 2000 + $darr[2];
	      else
		 $year = 1900 + $darr[2];

	      $str = sprintf("%04d-%02d-%02d", $year, $darr[1], $darr[0]);
	   break;

	   case "Y-m-d":
	      $str = $dt;
	   break;

	   case "m/d/Y":
	      $darr = explode("/", $dt);
	      $str = sprintf("%04d-%02d-%02d", $darr[2], $darr[0], $darr[1]);
	   break;

	   case "m/d/y":
	      $darr = explode("/", $dt);

	      if ($darr[2] < 50)
		 $year = 2000 + $darr[2];
	      else
		 $year = 1900 + $darr[2];

	      $str = sprintf("%04d-%02d-%02d", $year, $darr[0], $darr[1]);
	   break;

	   default:
	      $str = "ERROR";
	}

	return $str;
}

function GetShortDate($datum) {
	global $dtformatshort;

	switch ($dtformatshort) {
	   case "j.n.Y": $sd = "n.Y"; break;
	   case "d.m.Y": $sd = "m.Y"; break;
	   case "j.n.y": $sd = "n.y"; break;
	   case "d.m.y": $sd = "m.y"; break;
	   case "Y-m-d": $sd = "Y-m"; break;
	   case "m/d/Y": $sd = "m/Y"; break;
	   case "m/d/y": $sd = "m/y"; break;
	   default: $sd = "n.Y";
	}

	return gmdate($sd, $datum);
}

function GetShortDateYear($datum) {
	global $dtformatshort;

	switch ($dtformatshort) {
	   case "j.n.Y": $sd = "j.n.y"; break;
	   case "d.m.Y": $sd = "d.m.y"; break;
	   case "Y-m-d": $sd = "y-m-d"; break;
	   case "m/d/Y": $sd = "m/d/y"; break;
	   default: $sd = $dtformatshort;
	}

	return gmdate($sd, $datum);
}

function GetDateSep() {
	global $dtformatshort;

	switch ($dtformatshort) {
	   case "j.n.Y":
	   case "d.m.Y":
	   case "j.n.y":
	   case "d.m.y": return ".";
	   case "Y-m-d": return "-";
	   case "m/d/Y": 
	   case "m/d/y": return "/";
	   default: return ".";
	}
}

# Folgende Funktion gibt die Tage einer Periode wieder. Die Laenge einer
# Periode haengt von der eingestellten Einheit ab.
#
function GetPerLen($dt) {
	global $einheit;
	global $periode;

	$day = gmdate("j", $dt);
	$mon = gmdate("n", $dt);
	$year = gmdate("Y", $dt);

	switch ($einheit) {
	   case 1: return 3600;
	   case 2: return 86400;
	   case 3: return 86400 * 7;

	   case 4:	// Monat
	      $mon = gmdate("n", $dt);
	      $year = gmdate("Y", $dt);
	      return daysinmonth($mon, $year);

	   case 5:	// Quartal
	      $mon = gmdate("n", $dt);
	      $year = gmdate("Y", $dt);

	      if ($mon >= 1 && $mon <= 3) {
	         $dat1 = gmmktime(0, 0, 0, 1, 1, $year);
	         $dat2 = gmmktime(0, 0, 0, 3, 31, $year);
	      } else if ($mon >= 4 && $mon <= 6) {
	         $dat1 = gmmktime(0, 0, 0, 4, 1, $year);
	         $dat2 = gmmktime(0, 0, 0, 6, 30, $year);
	      } else if ($mon >= 7 && $mon <= 9) {
	         $dat1 = gmmktime(0, 0, 0, 7, 1, $year);
	         $dat2 = gmmktime(0, 0, 0, 9, 31, $year);
	      } else {
	         $dat1 = gmmktime(0, 0, 0, 10, 1, $year);
	         $dat2 = gmmktime(0, 0, 0, 12, 31, $year);
	      }

	      return ($dat2 - $dat1) / 86400;

	   case 6:	// Jahr
	      $dat1 = gmmktime(0, 0, 0, 1, 1, $year);
	      $dat2 = gmmktime(0, 0, 0, 12, 31, $year);
	      return ($dat2 - $dat1) / 86400;
	}

	return 0;
}

# Folgende Variablen stehen nach Einbindung dieses Includes "global" zur
# Verfuegung:
#
$ini_array = parse_ini_file("setup/settings.dat");
$periode = $ini_array['periode'];
$verteil = $ini_array['verteil'];
$einheit = $ini_array['einheit'];
$leneinheit = $ini_array['leneinheit'];
$phase = $ini_array['phase'];
$numphase = $ini_array['numphase'];
$dtformatshort = $ini_array['dtformatshort'];
$dtformatlong = $ini_array['dtformatlong'];
$datetime = $ini_array['datetime'];
$pmlight = $ini_array['pmlight'];
$role = $ini_array['role'];
$ist_erledigung = $ini_array['ist_erledigung'];
$menucolor = $ini_array['menucolor'];
$title_str = urldecode($ini_array['title']);
$ist_future = $ini_array['ist_future'];
$pj_lock = $ini_array['pj_lock'];
$statusbericht = $ini_array['statusbericht'];
$shortcuts = $ini_array['shortcuts'];
$verrmodul = $ini_array['verrmodul'];
$fixkunde = $ini_array['fixkunde'];
$stdwhg = $ini_array['stdwhg'];
$language = $ini_array['language'];
$pjcopy = $ini_array['pjcopy'];

if (!isset($phase))
   $phase = 0;

if (!isset($numphase))
   $numphase = 0;

if (!isset($dtformatshort))
   $dtformatshort = "j.n.Y";

if (!isset($dtformatlong))
   $dtformatlong = "d F Y";

if (!isset($datetime))
   $datetime = "d.m.Y H:i:s";

if (!isset($pmlight))
   $pmlight = 0;

if (!isset($role))
   $role = false;

if (!isset($menucolor))
   $menucolor = "black";

if (!isset($title_str))
   $title_str = "TheoPlan";

if (!isset($ist_erledigung))
   $ist_erledigung = 1;

if (!isset($ist_future))
   $ist_future = 0;

if (!isset($pj_lock))
   $pj_lock = 0;

if (!isset($statusbericht))
   $statusbericht = false;

if (!isset($shortcuts))
   $shortcuts = true;

if (!isset($verrmodul))
   $verrmodul = false;

if (!isset($fixkunde))
   $fixkunde = false;

if (!isset($stdwhg))
   $stdwhg = 42;		// EUR - Euro

if (!isset($language))
   $language = "ger";

# 1 = User waehlt das Datum aus
# 2 = Abschluss bis Ultimo Vormonat (15ner Regelung!)
# 3 = Abschluss bis letzter Teilprojektabschluss
#
if (!isset($pjcopy))
   $pjcopy = 3;

$VisualDate = GetVisualDate ($dtformatshort);
?>
